package com.xiaozhen.jt809.handler.server;

import com.xiaozhen.jt809.constant.JT809BusinessConstant;
import com.xiaozhen.jt809.domain.JT809MessageInfo;
import com.xiaozhen.jt809.handler.MyHandler;
import com.xiaozhen.jt809.utils.ByteArrayUtil;
import com.xiaozhen.jt809.utils.ResponseUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/16 17:11
 * @description:
 */
@Slf4j
@Service
public class UpDisconnectHandler implements MyHandler {

    @Override
    public void handler(ChannelHandlerContext ctx, JT809MessageInfo messageInfo) {
        log.info("下级平台请求注销主链路连接");
        int offset = 0;
        byte[] messageBody = messageInfo.getMessageBody();
        //用户名
        int userId = ByteArrayUtil.bytes2int(ByteArrayUtil.subBytes(messageBody, offset, 4));
        offset += 4;
        //密码
        String password = ByteArrayUtil.bytes2gbkString(ByteArrayUtil.subBytes(messageBody, offset, 8));
        password = password.replaceAll("\\u0000", "");
        log.info("主链路注销成功,用户名：{}，密码：{}", userId, password);
        //设置返回消息
        JT809MessageInfo responseMessage = ResponseUtil.responseMessage(messageInfo,
                JT809BusinessConstant.UP_DISCONNECT_RSP,
                new byte[0]);
        ctx.writeAndFlush(responseMessage);
    }
}
